import { getToken,setToken,removeToken,setTimestamp,removeTimestamp } from '@/utils/auth'
import { loginApi, getInfoApi } from '@/api/user'
import {getBaseInfoApi} from '@/api/employees'

const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  SET_TOKEN(state, payload) {
    state.token = payload
    setToken(payload)
    setTimestamp()
  },
  REMOVE_TOKEN(state) {
    state.token = '',
      removeToken(),
      removeTimestamp()
  },
  SET_USERINFO(state, payload) {
    state.userInfo = payload
  },
  REMOVE_USERINFO(state) {
    state.userInfo = {}
  },
}
const actions = {
  async login(context, payload) {
    const res = await loginApi(payload)
    context.commit('SET_TOKEN',res.data)
  },
  async getUserInfo(context) {
    const res = await getInfoApi()
    console.log(res);
    const baseRes = await getBaseInfoApi(res.data.userId)
    context.commit('SET_USERINFO', {...res.data,...baseRes.data})
    return res
  },
  logout(context) {
    context.commit('REMOVE_TOKEN'),
    context.commit('REMOVE_USERINFO')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}